﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Repair_RepMan_Handling : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string rep_id = "";
        string Pro_Num = "";
        rep_id = Request.QueryString["id"].ToString();
        string tabelall = "<table class='layui-table' width='80%'><thead><tr><th>报修单号</th><th>设备编号</th><th>所在位置</th><th>报修时间</th><th>报修原因</th><th>我要处理</th></tr></thead>";
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringProManage"].ConnectionString;
        try
        {
            conn.Open();
            if (rep_id!="")
            {
                SqlCommand cmdget_pro_num = new SqlCommand();
                string sqlproup = "select pro_num from repair where rep_id=@rep_id and flag=1";
                cmdget_pro_num.Connection = conn;
                cmdget_pro_num.CommandText = sqlproup;
                cmdget_pro_num.CommandType = CommandType.Text;
                SqlParameter ppid = new SqlParameter("@rep_id", SqlDbType.NVarChar, 10);
                ppid.Value = rep_id;
                cmdget_pro_num.Parameters.Add(ppid);
                using (SqlDataReader reader = cmdget_pro_num.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            Pro_Num = reader[0].ToString();
                        }
                        
                    }
                    else
                    {
                        Response.Write("<script>alert('没有读取到数值')</script>");
                    }
                }

                SqlCommand cmdup_pro_con = new SqlCommand();
                string sqlup_pro_con= "update Products set condition=N'正在维修' where pro_num=@pro_num";
                cmdup_pro_con.Connection = conn;
                cmdup_pro_con.CommandText = sqlup_pro_con;
                cmdup_pro_con.CommandType = CommandType.Text;
                SqlParameter ppro_num = new SqlParameter("@pro_num", SqlDbType.NVarChar, 10);
                ppro_num.Value = Pro_Num;
                cmdup_pro_con.Parameters.Add(ppro_num);
                cmdup_pro_con.ExecuteNonQuery();
            }
            


            SqlCommand cmddel = new SqlCommand();
            string SQLdel = "update Repair set condition=N'正在维修' where (rep_id=@rep_id)";
            cmddel.Connection = conn;
            cmddel.CommandText = SQLdel;
            cmddel.CommandType = CommandType.Text;
            //提交参数 SQL 语句中必须要有@参数
            SqlParameter pid = new SqlParameter("@rep_id", SqlDbType.NVarChar, 10);
            pid.Value = rep_id;
            cmddel.Parameters.Add(pid);
            
            cmddel.ExecuteNonQuery();

            



            SqlCommand cmd = new SqlCommand();
            string SQL = "select rep_id,pro_num,pro_location,date_inform,rep_cause from Repair where (flag=1 and condition=N'已报修')";
            cmd.Connection = conn;
            cmd.CommandText = SQL;
            cmd.CommandType = CommandType.Text;
            //提交参数 SQL 语句中必须要有@参数
            //SqlParameter ppro_num = new SqlParameter("@pro_num", SqlDbType.NVarChar, 10);
            //ppro_num.Value = keyword;
            //cmd.Parameters.Add(ppro_num);
            //int flag = 0;            //控制表头仅显示一次
            int k = 0;
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                int fieldcount = reader.FieldCount;      //取字段总数
                if (!reader.HasRows)            //有数据  ---行
                {
                    LabelText.Text = "<table class='layui-table' style='width: 70%; margin-left:15% '><tr><td>没有数据</td></tr>";
                }
                else
                {
                    string trd = "<tbody>";                     //存储数据
                    while (reader.Read())               //当有数据可读时
                    {
                        trd += "<tr>";         //数据行开始，行以tr起
                        for (int i = 0; i < fieldcount; i++)
                        {   //每一列的内容
                            trd += "<td>" + reader[i].ToString() + "</td>";
                        }

                        trd += "<td><a title = '我要维修' href = 'Repair_RepMan_Handling.aspx?id=" + reader[0].ToString() + "' ><i class='layui-icon'>&#xe631;</i></td>";  //行结束
                        k = k + 1;
                    }
                    LabelText.Text = tabelall + trd + "</tbody></table>";
                    MsgNum.Text = k.ToString();

                }
            }
        }

        finally
        {
            conn.Close();
        }
    }
}